//
//  JHPickerView.m
//  Job
//
//  Created by 欧松 on 2018/8/26.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "JHPickerView.h"
@interface JHPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong) UIPickerView *pcikerView;
@property(nonatomic,strong) UIView *contentView;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UIButton *cancelBtn;
@property(nonatomic,strong) UIButton *enterBtn;
@property(nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSDictionary *selectDict;
@end
@implementation JHPickerView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}

-(void)initSubView{
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];

    self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 250)];
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.contentView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.text = @"请选择";
    self.titleLabel.textColor = [UIColor lightGrayColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:self.titleLabel];
    
    self.cancelBtn = [UIButton new];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.cancelBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(removeView) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.cancelBtn];
    
    self.enterBtn = [UIButton new];
    [self.enterBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.enterBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.enterBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.enterBtn addTarget:self action:@selector(enterAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.enterBtn];
    
    UIView *view = [UIView new];
    view.backgroundColor = ViewBackgroundColor;
    [self.contentView addSubview:view];
    
    self.pcikerView = [UIPickerView new];
    self.pcikerView.delegate = self;
    self.pcikerView.dataSource = self;
    [self.contentView addSubview:self.pcikerView];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(10);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.centerY.equalTo(self.titleLabel);
    }];
    
    [self.enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cancelBtn.mas_bottom).offset(6);
        make.left.right.equalTo(self.contentView);
        make.height.mas_offset(@1);
    }];
    
    [self.pcikerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom);
        make.left.right.bottom.equalTo(self.contentView);
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.y = ScreenHeight - 250;
    }];
    
    if (self.dataSource.count > 8) {
        [self.pcikerView selectRow:8 inComponent:0 animated:YES];
    } else {
        [self.pcikerView selectRow:0 inComponent:0 animated:YES];
        
    }
}

-(void)enterAction{
    
    if (self.selectBlock) {
        self.selectBlock([self.selectDict[@"index"] integerValue], self.selectDict[@"data"]);
    }
    [self removeView];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSource.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataSource[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectDict = @{@"index":@(row),@"data":self.dataSource[row]};
}


-(void)removeView{
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.y = ScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

+(instancetype)show:(NSArray *)data{
    UIWindow *window = KeyWindow;
    JHPickerView *view = [[JHPickerView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    view.dataSource = data;
    if (data.count > 0) {
        view.selectDict = @{@"index":@(0),@"data":data[0]};
    }
    [window addSubview:view];
    return view;
}




@end
